<link rel="stylesheet" href="/plug/layui/css/layui.css">
<div style="margin: 40px;">
    <form class="layui-form" xmlns:th="http://www.thymeleaf.org">
        <input type="hidden" name="oldId" th:value="${student == null ? '' : student.id}">
        <input type="hidden" name="type" value="3">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">工号</label>
            <div class="layui-input-block">
                <input type="text" name="id" max="18" lay-verify="required" maxlength="100" placeholder="请输入学号" th:value="${student == null ? '' : student.id}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" maxlength="100" th:value="${student == null ? '' : student.name}" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" maxlength="100" placeholder="请输入密码(不填默认为123456)" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <select name="sex">
                    <option value="0" th:selected="${student != null && student.sex == 0}">女</option>
                    <option value="1" th:selected="${student != null && student.sex == 1}">男</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" lay-verify="required|phone" maxlength="100" placeholder="请输入联系电话" th:value="${student == null ? '' : student.phone}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">家庭地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" lay-verify="required" maxlength="100" placeholder="请输入家庭地址" th:value="${student == null ? '' : student.address}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">年级</label>
            <div class="layui-input-block">
                <select name="grade" lay-filter="grade">
                    <option th:each="data: ${gradeList}" th:value="${data.id}" th:text="${data.name}" th:selected="${student != null && student.gradeId == data.id}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">班级</label>
            <div class="layui-input-block">
                <select name="gradeClassId" lay-verify="required" lay-filter="class">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-input">
            <label class="layui-form-label">专业</label>
            <div class="layui-input-block">
                <input type="text" name="major" lay-verify="required" maxlength="100" placeholder="请输入专业" th:value="${student == null ? '' : student.major}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button type="button" onclick="editClose()" class="layui-btn layui-btn-warm">关闭</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/plug/layui/layui.all.js"></script>
<script>
    function editClose() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
    //加载layui
    layui.use(['layer', 'form'], function(){
        var layer = layui.layer
            ,form = layui.form;

        //layui提交
        form.on('submit(submit)', function(data){
            var id = $("input[name=id]").val();
            var ver = false;
            if(isNaN(id)) {
                layer.msg("请输入正确的工号！");
                return false;
            }
            var oldId = $("input[name=oldId]").val();
            $.ajaxSettings.async = false;
            if((oldId && oldId.trim() !== '' && id !== oldId) || (!oldId && oldId.trim() === '')) {
                $.get("/student/exist?id=" + id, function (res) {
                    if(res.code === 200) {
                        ver = res.data.exist
                    }
                })
            }
            if(ver) {
                layer.msg("该学号已存在！");
                return false;
            }
            $.ajaxSettings.async = true;
            $.ajax({
                data:JSON.parse(JSON.stringify(data.field)),
                type:"post",
                url:"/student/edit",
                success:function (rest) {
                    layer.msg(rest.msg);
                    if(rest.code === 200) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function () {
                    layer.msg("发生错误，请联系管理员");
                }
            });
            return false;
        });

        form.on('select(grade)', function (data) {
            $.get("/gradeClass/getClassByGrade?id=" + data.value, function (res) {
                if(res.code === 200) {
                    $('select[name=gradeClassId]').empty();
                    $(res.data.list).each(function () {
                        $('select[name=gradeClassId]').append(`<option value="${this.id}">${this.name}</option>`);
                    })
                    if([[${student != null}]]) {
                        $('select[name=gradeClassId]').val([[${student != null ? student.gradeClassId : ''}]]);
                    }
                    form.render('select');
                }
            })
        })
        $('select[name=grade]').siblings("div.layui-form-select").find('dl dd[lay-value=' + $("select[name=grade]").val() + ']').click();

        form.render('select');
    });
</script>